package com.easy_buy.mapper;


import com.easy_buy.domain.Category;
import com.easy_buy.domain.Commodity;
import com.easy_buy.domain.Commodityimage;
import com.easy_buy.domain.Preshop;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;

@Mapper
public interface StoreMapper {
    // 开发首页的sql
    @Select("SELECT * FROM category WHERE LENGTH(TCode) = 1")
    List<Category> getTypes();    // 获取商品大类名称

    @Select("SELECT * FROM commodity")
    List<Commodity> queryComsAll();   // 查询全部商品

    @Select("SELECT * FROM commodity WHERE LEFT(TCode,1) = #{tid}")
    List<Commodity> queryComsByTid(String tid);    // 按大类查询商品

    @Select("SELECT * FROM commodityimage WHERE Pid = #{pid}")
    Commodityimage queryImgByPid(int pid);     //获取商品图片信息



    //
    @Options(statementType = StatementType.CALLABLE)
    @Select("CALL Preshop_Insert(#{ucode},#{pid},#{Yes,jdbcType=BIT,mode=OUT})") // 提交yes=1
    void insertComToPreshop(String ucode, int pid);

    @Select("SELECT * FROM preshop WHERE UCode = #{ucode} AND Confirm = 0")
    List<Preshop> queryPresByUCode(String ucode);

    @Select("SELECT * FROM preshop WHERE UCode = #{ucode} AND Confirm = 1")
    List<Preshop> queryCfmsByUCode(String ucode);

    @Select("SELECT MAX(Oid)+1 FROM orders")
    Object getOid();

    //向订单表添加新生成的订单
    @Insert("INSERT INTO orders(Oid, UCode, PayMoney, PayTime) VALUES(#{oid}, #{ucode}, #{paymoney}, NOW())")
    int insertOrder(@Param("oid") int oid, @Param("ucode") String ucode, @Param("paymoney") float paymoney);//向订单表添加新生成的订单

    //更新商品表指定商品的库存
    @Update("UPDATE commodity SET Stocks=Stocks-#{cnum} WHERE Pid = #{pid}")
    int updateStocks(@Param("pid") int pid, @Param("cnum") int cnum);//更新商品表指定商品的库存

    //在购物车表里填写购买数量和订单号、置确认位
    @Update("UPDATE preshop SET CNum=#{cnum}, Confirm=1, Oid=#{oid}, estatus='待发货' WHERE UCode = #{ucode} AND Pid = #{pid}")
    int confirmPreshop(@Param("ucode") String ucode, @Param("pid") int pid, @Param("cnum") int cnum, @Param("oid") int oid);
}
